package com.jy.api.dao;

import com.jy.api.domain.CouponInstance;
import com.jy.api.domain.CouponInstanceStatus;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;

import java.util.Date;
import java.util.List;

/**
 * Created by lihu on 2017/11/17.
 */
public interface CouponInstanceDao extends CrudRepository<CouponInstance, String> {

    /**
     * 通过code 查询
     *
     * @param code
     * @return
     */
    CouponInstance findByCode(String code);


    List<CouponInstance> findByCouponIdAndAddTimeBetween(String couponId,Date start,Date end);

    /**
     * 查询会员对应店铺优惠卷信息
     *
     * @param memberId
     * @param storeId
     * @param pageable
     * @return
     */
    Page<CouponInstance> findListByOwnerIdAndStoreIdAndStautsAndEndTimeAfter(String memberId, String storeId,
                                                                             CouponInstanceStatus stauts, Date time,
                                                                             Pageable pageable);

    CouponInstance findByIdAndOpenId(String couponId,String ownerId);





    /**
     * 获取会员对应状态优惠卷信息
     *
     * @param memberId
     * @param stauts
     * @param time
     * @param pageable
     * @return
     */
    Page<CouponInstance> findListByOwnerIdAndStautsAndEndTimeAfter(String memberId, CouponInstanceStatus stauts,
                                                                   Date time, Pageable pageable);

    /**
     * 获取会员对应店铺对应状态优惠卷信息
     *
     * @param memberId
     * @param stauts
     * @param time
     * @param pageable
     * @return
     */
    Page<CouponInstance> findListByOwnerIdAndStautsAndStoreIdAndEndTimeAfter(String memberId, CouponInstanceStatus stauts,
                                                                             String storeId, Date time, Pageable pageable);

    /**
     * 店铺获取核销优惠卷信息
     *
     * @param storeId
     * @param stauts
     * @param pageable
     * @return
     */
    Page<CouponInstance> findListByStoreIdAndStautsOrderByValidateTimeDesc(String storeId,
                                                                           CouponInstanceStatus stauts, Pageable pageable);

    /**
     * 获取店铺优惠卷统计
     *
     * @param storeId
     * @return
     */
    @Query(value = "select coupon_id couponId,stauts,COUNT(*) number from coupon_instance WHERE" +
            " store_id=?1 and owner_id is not null group by couponId,stauts order BY number desc ", nativeQuery = true)
    List<Object[]> queryCouponCount(String storeId);

    /**
     * 获取时间段内优惠卷信息
     *
     * @param storeId
     * @param from
     * @param end
     * @return
     */
    @Query(value = "select coupon_id couponId,stauts,COUNT(*) number from coupon_instance WHERE" +
            " store_id=?1  and owner_id is not null and update_time BETWEEN ?2 and ?3 " +
            " group by couponId,stauts order BY number desc ", nativeQuery = true)
    List<Object[]> queryCouponCount(String storeId, Date from, Date end);


    /**
     * 查询同用户领取该类优惠卷分享信息
     *
     * @param openId
     * @param couponId
     * @return
     */
    List<CouponInstance> findByOpenIdAndCouponIdAndRefCouponCodeNotNull(String openId, String couponId);


    CouponInstance findByOpenIdAndCouponId(String openId, String couponId);

    long countByOwnerIdAndCouponId(String id, String couponId);

    List<CouponInstance> findByStoreIdAndOwnerId(String storeId,String ownerId);







}
